package com.yixuan.mt.common.service.impl_mysql;

import com.yixuan.mt.common.cache.EventsCache;
import com.yixuan.mt.common.entity.EventsMysql;
import com.yixuan.mt.common.entity.EventsRedis;
import com.yixuan.mt.common.exception.HttpBadRequestException;
import com.yixuan.mt.common.mapper.EventsMapper;
import com.yixuan.mt.common.pojo.EventsCachedData;
import com.yixuan.mt.common.pojo.EventsListInfo;
import com.yixuan.mt.common.pool.FlyDataSource;
import com.yixuan.mt.common.pool.RedisPool;
import com.yixuan.mt.common.request.EventsMysqlRequest;
import com.yixuan.mt.common.request.EventsRedisRequest;
import com.yixuan.mt.common.service.EventsService;
import com.yixuan.mt.common.utitls.EventsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class EventsServiceMysqlImpl implements EventsService {

    @Autowired
    private EventsMapper eventsMapper;

    @Autowired
    private EventsCache eventsCache;

    @Override
    public void postMysqlEvents(EventsMysqlRequest eventsMysqlRequest) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://" + eventsMysqlRequest.getUrl(), eventsMysqlRequest.getUsername(), eventsMysqlRequest.getPassword());
            connection.createStatement().execute(EventsUtils.getMysqlEventsTableSQL(eventsMysqlRequest.getDatabaseTable()));
            connection.close();
        } catch (SQLException e) {
            throw new HttpBadRequestException("创建MySQL事件错误：" + e.getMessage());
        }
        eventsMapper.insertMysqlEvents(eventsMysqlRequest);
        FlyDataSource flyDataSource = new FlyDataSource(1, "jdbc:mysql://" + eventsMysqlRequest.getUrl(), eventsMysqlRequest.getUsername(), eventsMysqlRequest.getPassword());
        flyDataSource.setEventsSQL(EventsUtils.getMysqlEventsInsertSQL(eventsMysqlRequest.getDatabaseTable()));
        eventsCache.addEventsCachedData(new EventsCachedData("mysql", eventsMysqlRequest.getName(), flyDataSource));
    }

    @Override
    public void postRedisEvents(EventsRedisRequest eventsRedisRequest) {
        try {
            Jedis jedis = new Jedis(eventsRedisRequest.getHost(), eventsRedisRequest.getPort());
            if (eventsRedisRequest.getPassword().isEmpty()) {
                eventsRedisRequest.setPassword(null);
            }
            if (eventsRedisRequest.getPassword() != null) {
                jedis.auth(eventsRedisRequest.getPassword());
            }
            jedis.ping();
            jedis.close();
        } catch (Exception e) {
            throw new HttpBadRequestException("创建Redis事件错误：" + e.getMessage());
        }
        eventsMapper.insertRedisEvents(eventsRedisRequest);
        eventsCache.addEventsCachedData(new EventsCachedData("redis", eventsRedisRequest.getName(), new RedisPool(eventsRedisRequest.getHost(), eventsRedisRequest.getPort(), eventsRedisRequest.getPassword())));
    }

    @Override
    public void deleteEvents(String type, String name) {
        if (type.equals("mysql")) {
            eventsMapper.deleteMysqlEvents(name);
        } else if (type.equals("redis")) {
            eventsMapper.deleteRedisEvents(name);
        }
        // 删除缓存
        eventsCache.removeEventsCachedData(type, name);
    }

    @Override
    public List<EventsListInfo> getEventsList() {
        List<EventsCachedData> eventsCachedDataList = eventsCache.getEventsCachedDataList();
        List<EventsListInfo> eventsListInfoList = new ArrayList<>();
        for (EventsCachedData eventsCachedData : eventsCachedDataList) {
            eventsListInfoList.add(new EventsListInfo(eventsCachedData.getType(), eventsCachedData.getName(), eventsCachedData.getDestination().isAlive()));
        }
        return eventsListInfoList;
    }

    @Override
    public List<EventsMysql> getMysqlEvents() {
        return eventsMapper.selectMysqlEvents();
    }

    @Override
    public List<EventsRedis> getRedisEvents() {
        return eventsMapper.selectRedisEvents();
    }
}
